/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2006-2007 Intel Corporation. All Rights Reserved.
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DV_VIDEO_ENCODER)

#include "umc_dv25_video_encoder.h"
#include "umc_dv_enc_dvsd_ntsc_segment_reader.h"
#include "umc_dv_enc_segment_writer.h"
#include "umc_video_data.h"
#include "umc_dv_enc_segment_compressor.h"

namespace UMC
{

DV25VideoEncoder::DV25VideoEncoder(void)
{
}

DV25VideoEncoder::~DV25VideoEncoder(void)
{
}

Status DV25VideoEncoder::Init(BaseCodecParams* pBaseParams)
{
    VideoEncoderParams *pParams = DynamicCast<VideoEncoderParams> (pBaseParams);
    //VideoEncoderParams *pGenParams = DynamicCast<VideoEncoderParams> (pBaseParams);

    if (pParams == NULL)
        return UMC_ERR_NULL_PTR;

    if (m_bIsInit)
        Close();

    //init Huffman Tables
    //377 ENCODE_ELEMENT entries
    //63 Ipp16s elements in TableMaxAmpOnRun table
    //62 (ENCODE_ELEMENT*) entries for encode table
    //4096 - reserved for alignment
    newHuffmanTablesBuffer = ippsMalloc_8u(377 * 4 + 63 * 2 + 62 * 4 + 4096);
    InitHuffmanTables(newHuffmanTablesBuffer);

    if( pParams->info.clip_info.width != 720 || (pParams->info.clip_info.height != 576 && pParams->info.clip_info.height != 480) )
      return UMC_ERR_INVALID_STREAM;

    if(UMC_OK != BaseCodec::Init(pBaseParams))
        return UMC_ERR_INIT;

    m_Params = *pParams;
    m_Params.info.stream_type = DIGITAL_VIDEO_SD;

    m_iMaxI = (480 == m_Params.info.clip_info.height) ? 10 : 12;

    // create DV segment write
    m_pDVWriter = new DVSegmentWriter;
    if (NULL == m_pDVWriter)
        return UMC_ERR_ALLOC;
    if (m_pDVWriter->InitWriter((480 == pParams->info.clip_info.height) ? 0 : 1, 0x7 /*DV25*/))
        return UMC_ERR_FAILED;

    // create segment compressor
    m_pSCompressor = new SegmentCompressor;
    if (NULL == m_pSCompressor)
        return UMC_ERR_ALLOC;
    if (m_pSCompressor->InitCompressor(6, m_pMemoryAllocator))
        return UMC_ERR_FAILED;

    // create segment reader
    m_pSReader = new DVSD_NTSC_SegmentReader( (480 == m_Params.info.clip_info.height) ? 10 : 12 );
    if (NULL == m_pSReader)
        return UMC_ERR_ALLOC;

    m_bIsInit = true;
    return UMC_OK;
}

}//namespace UMC

#endif //(UMC_ENABLE_DV_VIDEO_ENCODER)
